package com.wicket.okrframework.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrframework.base.service.MBehaviorTypeService;
import com.wicket.okrframework.base.service.dto.req.QueryBehaviorTypeDetailReq;
import com.wicket.okrframework.base.service.dto.req.QueryBehaviorTypeListReq;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.dal.mapper.mbg.BehaviorTypeMapper;
import com.wicket.okrframework.dal.po.mbg.Batch;
import com.wicket.okrframework.dal.po.mbg.BehaviorType;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import com.wicket.okrframework.base.service.dto.req.BatchQueryBehaviorTypeReq;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MBehaviorTypeServiceImpl
        extends ServiceImpl<BehaviorTypeMapper, BehaviorType>
        implements MBehaviorTypeService {

    @Autowired
    BehaviorTypeMapper behaviorTypeMapper;

    /**
   * 1-1-10修改行为类型[723]
   * gen by moon at 9/1/2022, 9:45:27 PM
   */
 @Trace(operationName = "1-1-10修改行为类型")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateBehaviorType(BehaviorType behaviorType) {
     behaviorType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(behaviorType!=null && behaviorType.getSubjectLifeCycle()!=null &&  behaviorType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            behaviorType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(behaviorType!=null && behaviorType.getSubjectLifeCycle()!=null && behaviorType.getSubjectLifeCycle().equals("ARCHIVING")) {
              behaviorType.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
          }
behaviorType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(behaviorType!=null && behaviorType.getSubjectLifeCycle()!=null && behaviorType.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             behaviorType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
if(behaviorType!=null && behaviorType.getSubjectLifeCycle()!=null && behaviorType.getSubjectLifeCycle().equals("ARCHIVING")) {
              behaviorType.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
          }
        return super.updateById(behaviorType);
  }

    /**
   * 1-1-10新增行为类型[722]
   * gen by moon at 9/1/2022, 9:45:26 PM
   */
 @Trace(operationName = "1-1-10新增行为类型")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addBehaviorType(BehaviorType behaviorType) {
     behaviorType.setBehaviorTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
behaviorType.setIsArchive("FALSE");
behaviorType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
behaviorType.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(behaviorType!=null && behaviorType.getSubjectLifeCycle()!=null &&  behaviorType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            behaviorType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
behaviorType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
behaviorType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(behaviorType!=null && behaviorType.getSubjectLifeCycle()!=null && behaviorType.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             behaviorType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
behaviorType.setIsValid("FALSE");
behaviorType.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
behaviorType.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(behaviorType)){
        return behaviorType.getBehaviorTypeId();
     }else {
        return "-1";
     }
    
  }

    /**
   * 1-1-10查询行为类型详情[156]
   * gen by moon at 9/1/2022, 9:45:26 PM
   */
 @Trace(operationName = "1-1-10查询行为类型详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  BehaviorType  queryBehaviorTypeDetail(QueryBehaviorTypeDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<BehaviorType> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<BehaviorType> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                BehaviorType retData = retList.get(0);

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 1-1-10查询行为类型列表[4]
   * gen by moon at 9/1/2022, 9:45:25 PM
   */
 @Trace(operationName = "1-1-10查询行为类型列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<BehaviorType>  queryBehaviorTypeList(QueryBehaviorTypeListReq reqDto) {
     
        try {
            QueryWrapper<BehaviorType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BehaviorType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BehaviorType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BehaviorType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<BehaviorType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-1-10删除行为类型[2]
   * gen by moon at 9/1/2022, 9:45:25 PM
   */
 @Trace(operationName = "1-1-10删除行为类型")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  deleteBehaviorType(BehaviorType behaviorType) {
     
     if(super.save(behaviorType)){
        return behaviorType.getBehaviorTypeId();
     }else {
        return "-1";
     }
    
  }
/**
   * 1-1-10批量查询行为类型[5004]
   * gen by moon at 10/24/2022, 9:42:08 PM
   */
 @Trace(operationName = "1-1-10批量查询行为类型")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<BehaviorType>  batchQueryBehaviorType(BatchQueryBehaviorTypeReq reqDto) {

        try {
            QueryWrapper<BehaviorType> queryWrapper = new QueryWrapper<>();

            if(reqDto.getBehaviorTypeList()==null || reqDto.getBehaviorTypeList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("biz_scene_id", reqDto.getBehaviorTypeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BehaviorType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BehaviorType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BehaviorType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<BehaviorType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
}
